package org.lql.chaos.rocketmq.service.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: liangqinglong
 * @date: 2025-08-16 20:19
 * @description: 顺序消息生产者
 **/
@Slf4j
@Service
public class OrderProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送顺序消息
     *
     * @param topic   主题
     * @param tag     标签
     * @param msg     消息内容
     * @param hashKey 用于保证顺序的hash key
     */
    public void sendOrderMessage(String topic, String tag, String msg, String hashKey) {
        String destination = topic + ":" + tag;
        log.info("发送顺序消息：{}", msg);
        SendResult sendResult = rocketMQTemplate.syncSendOrderly(destination, msg, hashKey);
        log.info("顺序消息发送结果：{}", sendResult);
    }
}